//
//  CZStatusRetweetView.swift
//  GZWeibo009
//
//  Created by gzxzmac on 16/3/27.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

import SnapKit
import YYKit

/// 被转发微博的view
class CZStatusRetweetView: UIView {
    
    // MARK: - 属性
    
    /// 被转发的微博模型
    var retweetStatus: CZStatus? {
        didSet {
            retweetContentLabel.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus == nil ? 0 : CZStatusCellMargin
                make.top.equalTo(margin)
            }
            
            retweetPictureView.pictureURLs = retweetStatus?.storePictureURLs
            
            retweetPictureView.snp_updateConstraints(closure: { (make) -> Void in
                let margin = retweetStatus?.storePictureURLs == nil ? 0 : CZStatusCellMargin
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(margin)
            })
            
            self.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus == nil ? 0 : CZStatusCellMargin
                make.bottom.equalTo(retweetPictureView).offset(margin)
            }
            
            if retweetStatus == nil {
                retweetContentLabel.attributedText = nil
            } else {
                // 当别人给被转发的模型赋值后,将模型的内容赋给对应的view
                // @发微博人的名字: 微博内容
                let screen_name = retweetStatus?.user?.screen_name ?? ""
                
                let attrText = retweetStatus?.attrText ?? NSAttributedString(string: "")
                
                let nameAttr = NSMutableAttributedString(string: "@\(screen_name): ")
                nameAttr.appendAttributedString(attrText)
                
                retweetContentLabel.attributedText = nameAttr
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        preapreUI()
    }
    
    private func preapreUI() {
        backgroundColor = UIColor(white: 245 / 255.0, alpha: 1)
        
        // 添加子控件
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        /// 被转发文本
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CZStatusCellMargin)
        }
        
        /// 被转发的微博配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        // CZStatusRetweetView底部和retweetPictureView 重合,CZStatusRetweetView的高度根据子控件的内容来确定
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView).offset(CZStatusCellMargin).constraint
        }
    }

    // MARK: - 懒加载
    /// 被转发微博内容label
    private lazy var retweetContentLabel: FFLabel = {
        let label = FFLabel()
        
        label.font = UIFont.systemFontOfSize(CZStatusCellContentSize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        
        // 换行/折行宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * CZStatusCellMargin

        label.labelDelegate = self
        
        return label
    }()
    
    /// 微博配图
    private lazy var retweetPictureView: CZStatusPictureView = CZStatusPictureView()
}

extension CZStatusRetweetView: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        CZPrint(msg: "点击的文字text: \(text)")
    }
}
